package com.mcxx.modules.temporary.salary.service.read;

import com.alibaba.fastjson.JSONObject;
import com.mcxx.modules.miniSalvation.order.dto.OrderSalaryDTO;
import com.mcxx.modules.miniSalvation.salary.dto.MemberSalaryDTO;
import com.mcxx.modules.temporary.family.dto.TmpFamilyMemberDTO;
import com.mcxx.modules.temporary.family.service.read.TmpFamilyMemberHistoryReadService;
import com.mcxx.modules.temporary.family.service.read.TmpFamilyMemberReadService;
import com.mcxx.modules.temporary.order.dao.read.TmpOrderReadDao;
import com.mcxx.modules.temporary.order.dto.GoodsDTO;
import com.mcxx.modules.temporary.order.dto.TmpApprovalDTO;
import com.mcxx.modules.temporary.order.dto.TmpAuditDTO;
import com.mcxx.modules.temporary.order.dto.TmpOrderSalaryDTO;
import com.mcxx.modules.temporary.order.service.read.TmpOrderReadService;
import com.mcxx.modules.temporary.salary.dao.read.TmpMemberSalaryReadDao;
import com.mcxx.modules.temporary.salary.dto.TmpFamilySalaryDTO;
import com.mcxx.modules.temporary.salary.dto.TmpMemberSalaryDTO;
import com.mcxx.modules.temporary.salary.entity.TmpFamilySalaryEntity;
import com.mcxx.modules.temporary.salary.dao.read.TmpFamilySalaryReadDao;
import com.mcxx.util.CommonUtil;
import com.mcxx.util.Constant;
import com.yinhai.ta404.core.transaction.annotation.TaTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@TaTransactional(readOnly = true)
public class TmpFamilySalaryReadService {

    @Autowired
    private TmpFamilySalaryReadDao tmpFamilySalaryReadDao;
    @Autowired
    private TmpFamilyMemberReadService tmpFamilyMemberReadService;
    @Autowired
    private TmpFamilyMemberHistoryReadService tmpFamilyMemberHistoryReadService;


    public List<TmpFamilySalaryEntity> listAll() {
    	return tmpFamilySalaryReadDao.listAll();
    }



    public TmpFamilySalaryEntity getById(String id) {
    	return tmpFamilySalaryReadDao.getById(id);
    }


    public TmpAuditDTO getFirstAuditSalaryByOrderId(String orderId){
        return tmpFamilySalaryReadDao.getFirstAuditSalaryByOrderId(orderId);
    }


    public TmpAuditDTO getAuditSalaryByOrderId(String orderId){
        return tmpFamilySalaryReadDao.getAuditSalaryByOrderId(orderId);
    }


    public TmpApprovalDTO getApprovalSalaryByOrderId(String orderId){
        return tmpFamilySalaryReadDao.getApprovalSalaryByOrderId(orderId);
    }

    public TmpApprovalDTO getFamilySalaryByOrderId(String orderId){
        return tmpFamilySalaryReadDao.getFamilySalaryByOrderId(orderId);
    }




    public TmpAuditDTO getFamilySalaryInfo(String orderId) {
        TmpAuditDTO tmpAuditDTO = tmpFamilySalaryReadDao.getAuditSalaryByOrderId(orderId);
        tmpAuditDTO = CommonUtil.invalidArgs(tmpAuditDTO) ? new TmpAuditDTO() : tmpAuditDTO;

        if (!CommonUtil.invalidArgs(tmpAuditDTO.getKinds())) {
            List<GoodsDTO> goodsDTOList = JSONObject.parseArray(tmpAuditDTO.getKinds(), GoodsDTO.class);
            tmpAuditDTO.setGoodsList(goodsDTOList);
        }


        List<TmpFamilyMemberDTO> memberList = new ArrayList<>();
        if (!Constant.HIS_ABLE_STATUS.contains(tmpAuditDTO.getState())) {

            memberList = tmpFamilyMemberReadService.getByFamilyId(tmpAuditDTO.getFamilyId(), null);
        }else{

            memberList = tmpFamilyMemberHistoryReadService.getByFamilyId(tmpAuditDTO.getFamilyId(), tmpAuditDTO.getOrderId(), null);
        }


        tmpAuditDTO.setMemberList(memberList);

        return tmpAuditDTO;
    }


    public TmpFamilySalaryEntity getByOrderId(String orderId){
        return tmpFamilySalaryReadDao.getByOrderId(orderId);
    }
}
